<?php

use Symfony\Component\Filesystem\Filesystem;

 class EduSohoUpgrade extends AbstractUpdater
 {
     public function update()
     {
        $this->getConnection()->beginTransaction();
        try{
            $this->installRouting();
            $this->installAssets();
            $this->installTheme();
            $this->installBlocks();
            $this->getConnection()->commit();
        } catch(\Exception $e) {
            $this->getConnection()->rollback();
            throw $e;
        }
     }

    public function installRouting()
    {
        $config = <<<EOT

_plugin_gracefultheme_web:
    resource: "@GracefulThemeBundle/Resources/config/routing.yml"
    prefix:   /

_plugin_gracefultheme_admin:
    resource: "@GracefulThemeBundle/Resources/config/routing_admin.yml"
    prefix:   /admin
EOT;
        $filepath = $this->kernel->getParameter('kernel.root_dir') . '/config/routing_plugins.yml';
        file_put_contents($filepath, $config, FILE_APPEND);
    }

    private function installAssets()
    {
        $rootDir = realpath($this->kernel->getParameter('kernel.root_dir') . '/../');

        $originDir = "{$rootDir}/plugins/GracefulTheme/GracefulThemeBundle/Resources/public";
        $targetDir = "{$rootDir}/web/bundles/gracefultheme";

        $filesystem = new Filesystem();

        if ($filesystem->exists($targetDir)) {
            $filesystem->remove($targetDir);
        }

        $filesystem->mirror($originDir, $targetDir, null, array('override' => true, 'delete' => true));
    }

    private function installTheme()
    {
        $rootDir = realpath($this->kernel->getParameter('kernel.root_dir') . '/../');

        $originDir = "{$rootDir}/plugins/GracefulTheme/theme";
        $targetDir = "{$rootDir}/web/themes/graceful";

        $filesystem = new Filesystem();

        if ($filesystem->exists($targetDir)) {
            $filesystem->remove($targetDir);
        }

        $filesystem->mirror($originDir, $targetDir, null, array('override' => true, 'delete' => true));
    }

    private function installBlocks()
    {
        $blockService = $this->createService('Content.BlockService');

        $block = $blockService->getBlockByCode('graceful:home_top_banner');
        if (empty($block)) {
            $block = $blockService->createBlock(array(
                'code'=>'graceful:home_top_banner',
                'title'=>'雅致简洁主题：首页头部图片轮播'
            ));
        }

        $content = <<<'EOD'
<a href="#"><img src="/themes/graceful/img/slide-1.png"></a>
<a href="#"><img src="/themes/graceful/img/slide-2.png"></a>
<a href="#"><img src="/themes/graceful/img/slide-3.png"></a>
EOD;
        $blockService->updateContent($block['id'], $content);

        $block = $blockService->getBlockByCode('graceful:bottom_info');
        if (empty($block)) {
            $block = $blockService->createBlock(array(
                'code'=>'graceful:bottom_info',
                'title'=>'雅致简洁主题：页脚信息区'
            ));
        }

        $content = <<<'EOD'
<dl>
    <dt>新手指南</dt>
    <dd><a href="#">购买流程</a></dd>
    <dd><a href="#">如何学习</a></dd>
    <dd><a href="#">会员制度</a></dd>
    <dd><a href="#">常见问题</a></dd>
</dl>

<dl>
    <dt>我是老师</dt>
    <dd><a href="#">如何发布课程</a></dd>
    <dd><a href="#">如何上传课件</a></dd>
    <dd><a href="#">如何录入题目</a></dd>
    <dd><a href="#">如何创建直播课</a></dd>
</dl>

<dl>
    <dt>我是学生</dt>
    <dd><a href="#">如何提问</a></dd>
    <dd><a href="#">如何做笔记</a></dd>
    <dd><a href="#">如何参加考试</a></dd>
    <dd><a href="#">如何查看资料</a></dd>
</dl>

<dl>
    <dt>账户管理</dt>
    <dd><a href="#">修改邮箱</a></dd>
    <dd><a href="#">找回密码</a></dd>
    <dd><a href="#">设置头像</a></dd>
    <dd><a href="#">实名认证</a></dd>
</dl>

<dl>
    <dt>关于我们</dt>
    <dd><a href="#">关于我们</a></dd>
    <dd><a href="#">联系我们</a></dd>
    <dd><a href="#">新浪微博</a></dd>
    <dd><a href="#">腾讯微博</a></dd>
</dl>
EOD;
        $blockService->updateContent($block['id'], $content);

    }

 }


 abstract class AbstractUpdater
 {
    protected $kernel;
    public function __construct ($kernel)
    {
        $this->kernel = $kernel;
    }

    public function getConnection()
    {
        return $this->kernel->getConnection();
    }

    protected function createService($name)
    {
        return $this->kernel->createService($name);
    }

    protected function createDao($name)
    {
        return $this->kernel->createDao($name);
    }

    abstract public function update();
   
 }